﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfCharting.Model.Style
{
    public class ChartStyle
    {
        private double xmin = 0;
        private double xmax = 1;
        private double ymin = 0;
        private double ymax = 1;

        private ChartGridLinePattern gridlinePattern;
        private ChartGridLine gridLine;

        Brush gridlineColor = Brushes.LightGray;

        private Canvas chartCanvas;

        public ChartStyle()
        {
        }

        public Canvas ChartCanvas
        {
            get { return chartCanvas; }
            set { chartCanvas = value; }
        }

        public double Xmin
        {
            get { return xmin; }
            set { xmin = value; }
        }

        public double Xmax
        {
            get { return xmax; }
            set { xmax = value; }
        }

        public double Ymin
        {
            get { return ymin; }
            set { ymin = value; }
        }

        public double Ymax
        {
            get { return ymax; }
            set { ymax = value; }
        }

        public Brush GridLineColor
        {
            get { return gridlineColor; }
            set { gridlineColor = value; }
        }


        public Point NormalizePoint(Point point)
        {
            if (double.IsNaN(ChartCanvas.Width))
                ChartCanvas.Width = 270;
            if (double.IsNaN(ChartCanvas.Height))
                ChartCanvas.Height = 250;
            Point result = new Point();
            result.X = (point.X - Xmin) * ChartCanvas.Width / (Xmax - Xmin);
            result.Y = ChartCanvas.Height - (point.Y - Ymin) * ChartCanvas.Height / (Ymax - Ymin);
            return result;
        }

        public static double OptimalSpacing(double original)
        {
            double[] da = { 1.0, 2.0, 5.0 };
            double multiplier = Math.Pow(10, Math.Floor(Math.Log(original) / Math.Log(10)));
            double dmin = 100 * multiplier;
            double spacing = 0.0;
            double mn = 100;

            foreach (double d in da)
            {
                double delta = Math.Abs(original - d * multiplier);
                if (delta < dmin)
                {
                    dmin = delta;
                    spacing = d * multiplier;
                }
                if (d < mn)
                {
                    mn = d;
                }
            }

            if (Math.Abs(original - 10 * mn * multiplier) < Math.Abs(original - spacing))
                spacing = 10 * mn * multiplier;

            return spacing;
        }

        public ChartGridLinePattern GridLinePattern
        {
            get { return gridlinePattern; }
            set { gridlinePattern = value; }
        }

        public ChartGridLine GridLine
        {
            get { return gridLine; }
            set { gridLine = value; }
        }

        public enum ChartGridLinePattern
        {
            None = 0,
            Solid = 1,
            Dash = 2,
            Dot = 3,
            DashDot = 4
        }

        public enum ChartGridLine
        {
            None = 0,
            Vertical = 1,
            Horizontal = 2,
            VerticalHorizontal = 3,
        }
    }
}
